<template>
  <div class="dialog">
    <el-dialog :title="'岗位' + titleText" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="formData.postName" placeholder="请输入岗位名称" clearable />
        </el-form-item>
        <el-form-item label="所属部门" prop="departmentId">
          <el-select class="w100p" filterable clearable v-model="formData.departmentId" placeholder="请选择部门">
            <el-option v-for="(item, index) of departmentList" :key="index + 'department'" :label="item.departmentName"
              :value="item.departmentId" />
          </el-select>
        </el-form-item>
        <el-form-item label="岗位职责" prop="benchmarkPostResp">
          <el-input v-model="formData.benchmarkPostResp" placeholder="请输入岗位职责" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import DepartmentApi from "@/api/basic-info-manage/department";
import PostApi from "@/api/basic-info-manage/post";

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
  },
  data() {
    return {
      formData: {},
      rules: {
        postName: [
          { required: true, message: "请输入岗位名称", trigger: ["change", "blur"] }
        ],
        departmentId: [
          { required: true, message: "请选择所属部门", trigger: ["change", "blur"] }
        ],
        // benchmarkPostResp: [
        //   { required: true, message: "请选择出库仓库", trigger: ["change", "blur"] }
        // ]
      },
      // 可选部门
      departmentList: [],
      showDialog: this.show,
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
      this.queryDepartment();
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    },
  },
  created() {
    this.queryDepartment()
  },
  mounted() {
  },
  methods: {
    // 查询备件
    queryDepartment() {
      // 获取数据
      let param = {
        page: 1,
        size: 1000
      };
      DepartmentApi.queryPageByCondition(param).then(res => {
        if (res.success) {
          let data = res.data;
          this.departmentList = data.content;
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            PostApi.edit(this.formData).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error("系统错误,修改失败");
              }
            });
          } else if (this.nowChanceStau === "add") {
            PostApi.add(this.formData).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else {
                this.$message.error("系统错误,修改失败");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-table th {
  color: #000;
  background-color: rgba(120, 182, 193, 0.4) !important;
}

::v-deep .el-table .el-table__row--striped {
  color: #000;
  background-color: rgba(186, 186, 186, 0.4) !important;
}

::v-deep.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group,
.el-table th.is-leaf {
  border-color: #D3D3D3 !important;
}

::v-deep .el-table th.is-leaf {
  border-color: #D3D3D3 !important;
}

::v-deep .el-table__empty-text {
  color: #000;
}

::v-deep .el-table {
  color: #000;
}

.addInfo {
  width: 100%;
  margin: 10px;
  background-color: #237FC3;
  color: #fff;
  font-size: 18px;
}

.el-form {
  margin-top: 10px;
}
</style>
